import { DefaultFactory } from '../Http'
import { StorageUtil } from '@/storage/StorageUtil'
import { route } from "@/route/Route"

import "reflect-metadata";
// #// #ifdef MP-WEIXIN

Object.getOwnPropertyNames(global.Reflect).forEach((key) => {
  Object.defineProperty(Reflect, key, {
    value: global.Reflect[key],
  });
})
// #endif

export function Post(url: string, logined: boolean = false, analysisRes: boolean = true) {
	return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) { // 返回的target就是调用本装饰器的类

		//   返回一个新的 descriptor
		return {
			...descriptor,
			value: function(...args: any) { // 重写方法
			
				const http = DefaultFactory.getInstance()
				let option = {};
				let tempUrl = url;
				
				//获取参数注解
				let requestBodyParameterNums: number = Reflect.getOwnMetadata(requestBodyMetadataKey, target, propertyKey);
				let requestSuccessParameterNums: number = Reflect.getOwnMetadata(requestSuccessMetadataKey, target, propertyKey);
				let requestErrorParameterNums: number = Reflect.getOwnMetadata(requestErrorMetadataKey, target, propertyKey);
				let requestCompleteParameterNums: number = Reflect.getOwnMetadata(requestCompleteMetadataKey, target, propertyKey);
				let responseCodeParameterNums: any[] = Reflect.getOwnMetadata(responseCodeMetadataKey, target, propertyKey);
				let requestPathParameterNums: any[] = Reflect.getOwnMetadata(requestPathParameterMetadataKey, target, propertyKey);
				let requestUrlParameterNums: number = Reflect.getOwnMetadata(requestUrlParameterMetadataKey, target, propertyKey);
				let requestHeaderParameterNums: number = Reflect.getOwnMetadata(requestHeaderMetadataKey, target, propertyKey);
				let requestContentTypeParameterNums: number = Reflect.getOwnMetadata(requestContentTypeMetadataKey, target, propertyKey);
				//处理带参数PATH
				if (requestPathParameterNums !== undefined) {
					for(let requestPathParameter in requestPathParameterNums){
						let value =	args[requestPathParameter.index]
						let key = requestPathParameter.key
						tempUrl = tempUrl.slice("{"+key+"}",value)
					}
				}
				//处理URL参数
				if (requestUrlParameterNums !== undefined) {
					let requestUrlParameter:any = args[requestUrlParameterNums]
					for (let key in requestUrlParameter) {
					        console.log(requestUrlParameter[key]) //不报错
							if(tempUrl.indexOf("?") === -1){
								tempUrl = tempUrl + "?" + key + "=" + requestUrlParameter[key]
							}else{
								tempUrl = tempUrl + "&" + key + "=" + requestUrlParameter[key]
							}
					    }
				}
				
				//处理header
				if (requestHeaderParameterNums !== undefined) {
					let parameter:any = args[requestHeaderParameterNums]
					let header = option.header || {}
					header = {...header,...parameter}
					option.header = header
				}
				
				//处理登录token
				if(logined){
					let header = option.header || {}
					header.Authorization = StorageUtil.getStorage(StorageUtil.TOKEN)
					option.header = header
				}

				if (requestBodyParameterNums !== undefined) {
					option.data = args[requestBodyParameterNums]
				}

				//处理ContentType
				if (requestContentTypeParameterNums !== undefined) {
					let parameter:any = args[requestContentTypeParameterNums]
					let header = option.header || {}
					header['content-type'] = parameter
					option.header = header
				}

				if (requestBodyParameterNums !== undefined) {
					option.data = args[requestBodyParameterNums]
				}

				if (requestSuccessParameterNums !== undefined) {
					option.success = function(res) {
						if(res.statusCode === 401){
							StorageUtil.setStorage(StorageUtil.TOKEN,"")
							StorageUtil.setStorage(StorageUtil.USER_ID,"")
							StorageUtil.setStorage(StorageUtil.USER,{})
							route.reLaunchTo({name:'login'})
							return
						}
						if (analysisRes === false) {
							args[requestSuccessParameterNums](res.data)
						} else {
							if (res.data.status == 200) {
								args[requestSuccessParameterNums](res.data.data)
							} else {
								//处理不同code值回调
								if (responseCodeParameterNums !== undefined) {
									if(responseCodeParameterNums.indexOf(res.data.status) == -1){
										if (requestErrorParameterNums !== undefined) {
											args[requestErrorParameterNums](res.data)
										}
									}else{
										for(let responseCode in responseCodeParameterNums){
											if(res.data.status === responseCode.status){
												args[responseCode.index](res.data)
												break
											}
										}
									}
								}else{
									if (requestErrorParameterNums !== undefined) {
										uni.showToast({
											title: res.data.message,
											duration: 2000,
											icon:'error'
										});
										args[requestErrorParameterNums](res.data)
										
									}
								}
							}
						}
					}
				}
				if (requestErrorParameterNums !== undefined) {
					option.error = function(res) {
						args[requestErrorParameterNums](res.data)
					}
				}
				if (requestCompleteParameterNums !== undefined) {
					option.complete = function(res) {
						args[requestCompleteParameterNums](res.data)
					}
				}
				
				//添加BASE_PATH
				if(process.env.VUE_APP_TICK_HTTP__BASE_PATH){
					tempUrl = process.env.VUE_APP_TICK_HTTP__BASE_PATH + tempUrl
				}
				option.url = tempUrl;
				
				http.post(option)


			}

		};
	}
}
export function Put(url: string, logined: boolean = false, analysisRes: boolean = true) {
	return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) { // 返回的target就是调用本装饰器的类

		//   返回一个新的 descriptor
		return {
			...descriptor,
			value: function(...args: any) { // 重写方法
			
				const http = DefaultFactory.getInstance()
				let option = {};
				let tempUrl = url;
				
				//获取参数注解
				let requestBodyParameterNums: number = Reflect.getOwnMetadata(requestBodyMetadataKey, target, propertyKey);
				let requestSuccessParameterNums: number = Reflect.getOwnMetadata(requestSuccessMetadataKey, target, propertyKey);
				let requestErrorParameterNums: number = Reflect.getOwnMetadata(requestErrorMetadataKey, target, propertyKey);
				let requestCompleteParameterNums: number = Reflect.getOwnMetadata(requestCompleteMetadataKey, target, propertyKey);
				let responseCodeParameterNums: any[] = Reflect.getOwnMetadata(responseCodeMetadataKey, target, propertyKey);
				let requestPathParameterNums: any[] = Reflect.getOwnMetadata(requestPathParameterMetadataKey, target, propertyKey);
				let requestUrlParameterNums: number = Reflect.getOwnMetadata(requestUrlParameterMetadataKey, target, propertyKey);
				let requestHeaderParameterNums: number = Reflect.getOwnMetadata(requestHeaderMetadataKey, target, propertyKey);
				let requestContentTypeParameterNums: number = Reflect.getOwnMetadata(requestContentTypeMetadataKey, target, propertyKey);
				//处理带参数PATH
				if (requestPathParameterNums !== undefined) {
					for(let requestPathParameter in requestPathParameterNums){
						let value =	args[requestPathParameter.index]
						let key = requestPathParameter.key
						tempUrl = tempUrl.slice("{"+key+"}",value)
					}
				}
				//处理URL参数
				if (requestUrlParameterNums !== undefined) {
					let requestUrlParameter:any = args[requestUrlParameterNums]
					for (let key in requestUrlParameter) {
					        console.log(requestUrlParameter[key]) //不报错
							if(tempUrl.indexOf("?") === -1){
								tempUrl = tempUrl + "?" + key + "=" + requestUrlParameter[key]
							}else{
								tempUrl = tempUrl + "&" + key + "=" + requestUrlParameter[key]
							}
					    }
				}
				
				//处理header
				if (requestHeaderParameterNums !== undefined) {
					let parameter:any = args[requestHeaderParameterNums]
					let header = option.header || {}
					header = {...header,...parameter}
					option.header = header
				}
				
				//处理登录token
				if(logined){
					let header = option.header || {}
					header.Authorization = StorageUtil.getStorage(StorageUtil.TOKEN)
					option.header = header
				}

				if (requestBodyParameterNums !== undefined) {
					option.data = args[requestBodyParameterNums]
				}

				//处理ContentType
				if (requestContentTypeParameterNums !== undefined) {
					let parameter:any = args[requestContentTypeParameterNums]
					let header = option.header || {}
					header['content-type'] = parameter
					option.header = header
				}

				if (requestBodyParameterNums !== undefined) {
					option.data = args[requestBodyParameterNums]
				}

				if (requestSuccessParameterNums !== undefined) {
					option.success = function(res) {
						if(res.statusCode === 401){
							StorageUtil.setStorage(StorageUtil.TOKEN,"")
							StorageUtil.setStorage(StorageUtil.USER_ID,"")
							StorageUtil.setStorage(StorageUtil.USER,{})
							route.reLaunchTo({name:'login'})
							return
						}
						if (analysisRes === false) {
							args[requestSuccessParameterNums](res.data)
						} else {
							if (res.data.status == 200) {
								args[requestSuccessParameterNums](res.data.data)
							} else {
								//处理不同code值回调
								if (responseCodeParameterNums !== undefined) {
									if(responseCodeParameterNums.indexOf(res.data.status) == -1){
										if (requestErrorParameterNums !== undefined) {
											args[requestErrorParameterNums](res.data)
										}
									}else{
										for(let responseCode in responseCodeParameterNums){
											if(res.data.status === responseCode.status){
												args[responseCode.index](res.data)
												break
											}
										}
									}
								}else{
									if (requestErrorParameterNums !== undefined) {
										args[requestErrorParameterNums](res.data)
									}
								}
							}
						}
					}
				}
				if (requestErrorParameterNums !== undefined) {
					option.error = function(res) {
						args[requestErrorParameterNums](res.data)
					}
				}
				if (requestCompleteParameterNums !== undefined) {
					option.complete = function(res) {
						args[requestCompleteParameterNums](res.data)
					}
				}
				
				//添加BASE_PATH
				if(process.env.VUE_APP_TICK_HTTP__BASE_PATH){
					tempUrl = process.env.VUE_APP_TICK_HTTP__BASE_PATH + tempUrl
				}
				option.url = tempUrl;
				
				http.put(option)


			}

		};
	}
}
export function Delete(url: string, logined: boolean = false, analysisRes: boolean = true) {
	return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) { // 返回的target就是调用本装饰器的类

		//   返回一个新的 descriptor
		return {
			...descriptor,
			value: function(...args: any) { // 重写方法
			
				const http = DefaultFactory.getInstance()
				let option = {};
				let tempUrl = url;
				
				//获取参数注解
				let requestBodyParameterNums: number = Reflect.getOwnMetadata(requestBodyMetadataKey, target, propertyKey);
				let requestSuccessParameterNums: number = Reflect.getOwnMetadata(requestSuccessMetadataKey, target, propertyKey);
				let requestErrorParameterNums: number = Reflect.getOwnMetadata(requestErrorMetadataKey, target, propertyKey);
				let requestCompleteParameterNums: number = Reflect.getOwnMetadata(requestCompleteMetadataKey, target, propertyKey);
				let responseCodeParameterNums: any[] = Reflect.getOwnMetadata(responseCodeMetadataKey, target, propertyKey);
				let requestPathParameterNums: any[] = Reflect.getOwnMetadata(requestPathParameterMetadataKey, target, propertyKey);
				let requestUrlParameterNums: number = Reflect.getOwnMetadata(requestUrlParameterMetadataKey, target, propertyKey);
				let requestHeaderParameterNums: number = Reflect.getOwnMetadata(requestHeaderMetadataKey, target, propertyKey);
				let requestContentTypeParameterNums: number = Reflect.getOwnMetadata(requestContentTypeMetadataKey, target, propertyKey);
				//处理带参数PATH
				if (requestPathParameterNums !== undefined) {
					for(let requestPathParameter in requestPathParameterNums){
						let value =	args[requestPathParameter.index]
						let key = requestPathParameter.key
						tempUrl = tempUrl.slice("{"+key+"}",value)
					}
				}
				//处理URL参数
				if (requestUrlParameterNums !== undefined) {
					let requestUrlParameter:any = args[requestUrlParameterNums]
					for (let key in requestUrlParameter) {
					        console.log(requestUrlParameter[key]) //不报错
							if(tempUrl.indexOf("?") === -1){
								tempUrl = tempUrl + "?" + key + "=" + requestUrlParameter[key]
							}else{
								tempUrl = tempUrl + "&" + key + "=" + requestUrlParameter[key]
							}
					    }
				}
				
				//处理header
				if (requestHeaderParameterNums !== undefined) {
					let parameter:any = args[requestHeaderParameterNums]
					let header = option.header || {}
					header = {...header,...parameter}
					option.header = header
				}
				
				//处理登录token
				if(logined){
					let header = option.header || {}
					header.Authorization = StorageUtil.getStorage(StorageUtil.TOKEN)
					option.header = header
				}

				if (requestBodyParameterNums !== undefined) {
					option.data = args[requestBodyParameterNums]
				}

				//处理ContentType
				if (requestContentTypeParameterNums !== undefined) {
					let parameter:any = args[requestContentTypeParameterNums]
					let header = option.header || {}
					header['content-type'] = parameter
					option.header = header
				}

				if (requestBodyParameterNums !== undefined) {
					option.data = args[requestBodyParameterNums]
				}

				if (requestSuccessParameterNums !== undefined) {
					option.success = function(res) {
						if(res.statusCode === 401){
							StorageUtil.setStorage(StorageUtil.TOKEN,"")
							StorageUtil.setStorage(StorageUtil.USER_ID,"")
							StorageUtil.setStorage(StorageUtil.USER,{})
							route.reLaunchTo({name:'login'})
							return
						}
						if (analysisRes === false) {
							args[requestSuccessParameterNums](res.data)
						} else {
							if (res.data.status == 200) {
								args[requestSuccessParameterNums](res.data.data)
							} else {
								//处理不同code值回调
								if (responseCodeParameterNums !== undefined) {
									if(responseCodeParameterNums.indexOf(res.data.status) == -1){
										if (requestErrorParameterNums !== undefined) {
											args[requestErrorParameterNums](res.data)
										}
									}else{
										for(let responseCode in responseCodeParameterNums){
											if(res.data.status === responseCode.status){
												args[responseCode.index](res.data)
												break
											}
										}
									}
								}else{
									if (requestErrorParameterNums !== undefined) {
										args[requestErrorParameterNums](res.data)
									}
								}
							}
						}
					}
				}
				if (requestErrorParameterNums !== undefined) {
					option.error = function(res) {
						args[requestErrorParameterNums](res.data)
					}
				}
				if (requestCompleteParameterNums !== undefined) {
					option.complete = function(res) {
						args[requestCompleteParameterNums](res.data)
					}
				}
				
				//添加BASE_PATH
				if(process.env.VUE_APP_TICK_HTTP__BASE_PATH){
					tempUrl = process.env.VUE_APP_TICK_HTTP__BASE_PATH + tempUrl
				}
				option.url = tempUrl;
				
				http.delete(option)


			}

		};
	}
}
export function Get(url: string, logined: boolean = false, analysisRes: boolean = true) {
	return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) { // 返回的target就是调用本装饰器的类

		//   返回一个新的 descriptor
		return {
			...descriptor,
			value: function(...args: any) { // 重写方法
			
				const http = DefaultFactory.getInstance()
				let option = {};
				
				let tempUrl = url;
				//获取参数注解
				let requestBodyParameterNums: number = Reflect.getOwnMetadata(requestBodyMetadataKey, target, propertyKey);
				let requestSuccessParameterNums: number = Reflect.getOwnMetadata(requestSuccessMetadataKey, target, propertyKey);
				let requestErrorParameterNums: number = Reflect.getOwnMetadata(requestErrorMetadataKey, target, propertyKey);
				let requestCompleteParameterNums: number = Reflect.getOwnMetadata(requestCompleteMetadataKey, target, propertyKey);
				let responseCodeParameterNums: any[] = Reflect.getOwnMetadata(responseCodeMetadataKey, target, propertyKey);
				let requestPathParameterNums: any[] = Reflect.getOwnMetadata(requestPathParameterMetadataKey, target, propertyKey);
				let requestUrlParameterNums: number = Reflect.getOwnMetadata(requestUrlParameterMetadataKey, target, propertyKey);
				let requestHeaderParameterNums: number = Reflect.getOwnMetadata(requestHeaderMetadataKey, target, propertyKey);
				let requestContentTypeParameterNums: number = Reflect.getOwnMetadata(requestContentTypeMetadataKey, target, propertyKey);
				//处理带参数PATH
				if (requestPathParameterNums !== undefined) {
					for(let requestPathParameter in requestPathParameterNums){
						let value =	args[requestPathParameter.index]
						let key = requestPathParameter.key
						tempUrl = tempUrl.slice("{"+key+"}",value)
					}
				}
				//处理URL参数
				if (requestUrlParameterNums !== undefined) {
					let requestUrlParameter:any = args[requestUrlParameterNums]
					for (let key in requestUrlParameter) {
					        console.log(requestUrlParameter[key]) //不报错
							if(tempUrl.indexOf("?") === -1){
								tempUrl = tempUrl + "?" + key + "=" + requestUrlParameter[key]
							}else{
								tempUrl = tempUrl + "&" + key + "=" + requestUrlParameter[key]
							}
					    }
				}
				
				//处理header
				if (requestHeaderParameterNums !== undefined) {
					let parameter:any = args[requestHeaderParameterNums]
					let header = option.header || {}
					header = {...header,...parameter}
					option.header = header
				}

				//处理登录token
				if(logined){
					let header = option.header || {}
					header.Authorization = StorageUtil.getStorage(StorageUtil.TOKEN)
					option.header = header
				}
				
				if (requestBodyParameterNums !== undefined) {
					option.data = args[requestBodyParameterNums]
				}

				//处理ContentType
				if (requestContentTypeParameterNums !== undefined) {
					let parameter:any = args[requestContentTypeParameterNums]
					let header = option.header || {}
					header['content-type'] = parameter
					option.header = header
				}

				if (requestBodyParameterNums !== undefined) {
					option.data = args[requestBodyParameterNums]
				}

				if (requestSuccessParameterNums !== undefined) {
					option.success = function(res) {
						if(res.statusCode === 401){
							StorageUtil.setStorage(StorageUtil.TOKEN,"")
							StorageUtil.setStorage(StorageUtil.USER_ID,"")
							StorageUtil.setStorage(StorageUtil.USER,{})
							route.reLaunchTo({name:'login'})
							return
						}
						console.log(res)
						if (analysisRes === false) {
							args[requestSuccessParameterNums](res.data)
						} else {
							if (res.data.status == 200) {
								args[requestSuccessParameterNums](res.data.data)
							} else {
								//处理不同code值回调
								if (responseCodeParameterNums !== undefined) {
									if(responseCodeParameterNums.indexOf(res.data.status) == -1){
										if (requestErrorParameterNums !== undefined) {
											option.error = function(res) {
												args[requestErrorParameterNums](res.data)
											}
										}
									}else{
										for(let responseCode in responseCodeParameterNums){
											if(res.data.status === responseCode.status){
												args[responseCode.index](res.data)
												break
											}
										}
									}
								}else{
									if (requestErrorParameterNums !== undefined) {
										option.error = function(res) {
											args[requestErrorParameterNums](res.data)
										}
									}
								}
							}
						}
					}
				}
				if (requestErrorParameterNums !== undefined) {
					option.error = function(res) {
						console.log(res)
						args[requestErrorParameterNums](res)
					}
				}
				if (requestCompleteParameterNums !== undefined) {
					option.complete = function(res) {
						args[requestCompleteParameterNums](res.data)
					}
				}
				
				//添加BASE_PATH
				if(process.env.VUE_APP_TICK_HTTP__BASE_PATH){
					tempUrl = process.env.VUE_APP_TICK_HTTP__BASE_PATH + tempUrl
				}
				option.url = tempUrl;
				http.get(option)


			}

		};
	}
}
const requestBodyMetadataKey = Symbol("requestBody");
export function requestBody(target: Object, propertyKey: string | symbol, parameterIndex: number) {
	Reflect.defineMetadata(requestBodyMetadataKey, parameterIndex, target, propertyKey);
}
const requestSuccessMetadataKey = Symbol("requestSuccess");
export function success(target: Object, propertyKey: string | symbol, parameterIndex: number) {
	Reflect.defineMetadata(requestSuccessMetadataKey, parameterIndex, target, propertyKey);
}
const requestErrorMetadataKey = Symbol("requestError");
export function error(target: Object, propertyKey: string | symbol, parameterIndex: number) {
	Reflect.defineMetadata(requestErrorMetadataKey, parameterIndex, target, propertyKey);
}
const requestCompleteMetadataKey = Symbol("requestComplete");
export function complete(target: Object, propertyKey: string | symbol, parameterIndex: number) {
	Reflect.defineMetadata(requestCompleteMetadataKey, parameterIndex, target, propertyKey);
}

//不同code值装饰器
const responseCodeMetadataKey = Symbol("responseCode");
export function code(code:number | string) {
	return function(target: Object, propertyKey: string | symbol, parameterIndex: number) {
		let parameters: any[] = Reflect.getOwnMetadata(responseCodeMetadataKey, target, propertyKey) || [];
		parameters.push({
			index: parameterIndex,
			code: code
		});
		Reflect.defineMetadata(responseCodeMetadataKey, parameters, target, propertyKey);
	}
}
//UrlPath参数装饰器 例:/getById/{id}
const requestPathParameterMetadataKey = Symbol("requestPathParameter");
export function pathParameter(key: string) {
	return function(target: Object, propertyKey: string | symbol, parameterIndex: number) {
		let parameters: any[] = Reflect.getOwnMetadata(requestPathParameterMetadataKey, target, propertyKey) || [];
		parameters.push({
			index: parameterIndex,
			key: key
		});
		Reflect.defineMetadata(requestPathParameterMetadataKey, parameters, target, propertyKey);
	}
}
//Url参数装饰器 例getById?id=1
const requestUrlParameterMetadataKey = Symbol("requestUrlParameter");
export function urlParameter(target: Object, propertyKey: string | symbol, parameterIndex: number) {
	Reflect.defineMetadata(requestUrlParameterMetadataKey, parameterIndex, target, propertyKey);
}


//header装饰器 
const requestHeaderMetadataKey = Symbol("requestHeader");
export function header(target: Object, propertyKey: string | symbol, parameterIndex: number) {
	Reflect.defineMetadata(requestHeaderMetadataKey, parameterIndex, target, propertyKey);
}

//请求类型Content-Type 装饰器
const requestContentTypeMetadataKey = Symbol("requestContentType");
export function contentType(target: Object, propertyKey: string | symbol, parameterIndex: number) {
	Reflect.defineMetadata(requestContentTypeMetadataKey, parameterIndex, target, propertyKey);
}